package jianzhioffer;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

// 剑指 Offer 18. 删除链表的节点
public class DeleteNode {

    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put(1,1);
        ArrayList<Object> list = new ArrayList<>(3);


    }
    // 单指针，开辟了一个头结点
    public   ListNode deleteNode(ListNode head, int val) {
        ListNode h = new ListNode(0);
        h.next = head;
        ListNode p = h;
        while (p.next != null) {
            if (p.next.val == val) {
                p.next = p.next.next;
            } else {
                p = p.next;
            }
        }
        return h.next;
    }


    // 单指针，不开辟头结点
    public ListNode deleteNode2(ListNode head, int val) {
        if (head.val == val) {
            return head.next;
        }
        ListNode p = head;
        while (p.next != null) {
            if (p.next.val == val) {
                p.next = p.next.next;
            } else {
                p = p.next;
            }
        }
        return head;
    }
}
